﻿namespace PokerSimulator
{
	using System;
	using System.Collections;
	using System.Collections.Generic;
	using System.Collections.ObjectModel;

	public class HandCollection : IEnumerable<Hand>
	{
		private readonly List<Hand> _hands;

		public HandCollection()
		{
			_hands = new List<Hand>();
		}

		public HandCollection(int handCount)
		{
			_hands = new List<Hand>(handCount);
			for (int i = 0; i < handCount; i++)
			{
				_hands.Add(new Hand());
			}
		}

        public int Count
		{
			get { return _hands.Count; }
		}

		public Hand this[int index]
		{
			get { return _hands[index]; }
		}

		#region IEnumerable<Hand> Members

		public IEnumerator<Hand> GetEnumerator()
		{
			return _hands.GetEnumerator();
		}

		IEnumerator IEnumerable.GetEnumerator()
		{
			return _hands.GetEnumerator();
		}

		#endregion

		public void Add(Hand hand)
		{
			_hands.Add(hand);
		}

		public void Clear()
		{
			_hands.Clear();
		}

		public ReadOnlyCollection<string> Display()
		{
			_hands.Sort();
			_hands.Reverse();
			List<string> outputLines = new List<string>();
			for (int i = 0; i < _hands.Count; i++)
			{
				string output = string.Concat("Hand ", i + 1, ": ", _hands[i].Value, ", ", _hands[i].Description);
				outputLines.Add(output);
			}
			return outputLines.AsReadOnly();
		}

		public void Sort()
		{
			_hands.Sort(HandValueComparison());
		}

		private static Comparison<Hand> HandValueComparison()
		{
			return (a, b) => a.Value.CompareTo(b.Value);
		}
	}
}